package com.tencent.trtc.videocall;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.trtc.debug.Constant;

/**
 * TRTC视频通话的入口页面（可以设置房间id和用户id）
 *
 * - 可跳转TRTC视频通话页面{@link VideoCallingActivity}
 */

/**
 * Video Call Entrance View (set room ID and user ID)
 *
 * - Direct to the video call view: {@link VideoCallingActivity}
 */
public class VideoCallingEnterActivity extends AppCompatActivity {

    private EditText mEditInputUserId;
    private TextView mEditInputRoomId;
    private TextView mtv_enter_title;
    private EditText mEditInputPassword;
    private String PATH = "http://120.78.210.235:8080/myWeb";

    private String rname = "" ;
    private String rid = "" ;
    private String password = "";
    private String result = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.videocall_activit_enter);
        getSupportActionBar().hide();
        mEditInputUserId = findViewById(R.id.et_input_username);
        mEditInputPassword = findViewById(R.id.et_input_password);
        mEditInputRoomId = findViewById(R.id.et_input_room_id);
        mtv_enter_title = findViewById(R.id.tv_enter_title);

        mEditInputUserId.setVisibility(View.INVISIBLE);


        findViewById(R.id.btn_enter_room).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                password = mEditInputPassword.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String path = PATH + "/roomlogin?RoomName="+rname+"&Rpassword="+password;
                        result = WebServiceGet.executeHttpGet(path);
                    }
                }).start();
                while(result.equals(""))
                { }
                if(result.equals("1")){
                    startEnterRoom();
                }else{
                    Toast.makeText(VideoCallingEnterActivity.this,"密码错误！进入房间失败！",Toast.LENGTH_LONG).show();
                }
            }
        });
        findViewById(R.id.rl_entrance_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideInput();
            }
        });

        //取出房间名，房间号
        Intent intent = getIntent();
        rname = intent.getStringExtra("Enter_RoomName");
        rid = intent.getStringExtra("Enter_RoomID");

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mEditInputRoomId.setText(rid);
        mtv_enter_title.setText(rname);
        String time = String.valueOf(System.currentTimeMillis());
        String userId = time.substring(time.length() - 8);
        mEditInputUserId.setText(userId);
    }

    private void startEnterRoom() {
        if (TextUtils.isEmpty(mEditInputUserId.getText().toString().trim())
                || TextUtils.isEmpty(mEditInputRoomId.getText().toString().trim())) {
            Toast.makeText(VideoCallingEnterActivity.this, getString(R.string.videocall_room_input_error_tip), Toast.LENGTH_LONG).show();
            return;
        }
        Intent intent = new Intent(VideoCallingEnterActivity.this, VideoCallingActivity.class);
        intent.putExtra(Constant.ROOM_ID, mEditInputRoomId.getText().toString().trim());
        intent.putExtra(Constant.USER_ID, mEditInputUserId.getText().toString().trim());
        startActivity(intent);
    }

    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

}
